package com.fosu.dao;

import com.fosu.common.ResponseResult;
import com.fosu.domain.User;
import com.fosu.vo.Reply;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

/**
 * 用户持久层
 *
 * @author manno
 * @date 2020/10/9
 */
@Mapper
@Repository
public interface UserDao {


    /**
     * 查找所有用户
     *
     * @return List<User> 用户对象集合
     */
    @Select("select * from user")
    @Results(id = "userMap", value = {
            @Result(id = true, property = "userId", column = "user_id"),
            @Result(property = "phone", column = "phone"),
            @Result(property = "wechatId", column = "wechat_id"),
            @Result(property = "realName", column = "real_name"),
            @Result(property = "nickName", column = "nick_name"),
            @Result(property = "avatarUrl", column = "avatar_url"),
            @Result(property = "openId", column = "open_id"),
            @Result(property = "loginTime", column = "login_time"),
            @Result(property = "lastLoginTime", column = "last_login_time"),
            @Result(property = "gender", column = "gender"),
            @Result(property = "city", column = "city"),
            @Result(property = "province", column = "province"),
            @Result(property = "country", column = "country"),
            @Result(property = "userCampus"
                    , column = "user_id"
                    , one = @One(select = "com.fosu.dao.CampusDao.findCampusByUserId")),
    }
    )
    List<User> findAll();


    /**
     * 根据用户id查找特定用户
     *
     * @param userId 用户id
     * @return User 用户对象
     */
    @Select("select * from user where user_id=#{userId}")
    @ResultMap(value = {"userMap"})
    User findUserById(int userId);


    /**
     * 根据openid查找用户
     * @param openId
     * @return User
     */
    @Select("select * from user where open_id=#{openId}")
    @ResultMap(value = {"userMap"})
    User findUserByOpenId(String openId);


    @Insert("insert into user(phone,wechat_id,real_name,nick_name,avatar_url,open_Id,login_time,last_login_time,gender,city,province,country) values(#{phone},#{wechatId},#{realName},#{nickName}," +
            "#{avatarUrl},#{openId},#{loginTime},#{lastLoginTime},#{gender},#{city},#{province},#{country})")

    void saveUser(User user);


    /**
     * 更新登录日期
     *
     * @param user
     */
    @Update("update user set login_time=#{loginTime},last_login_time=#{lastLoginTime} where user_id=#{userId}")
    void updateUser(User user);


    /**修改用户名
     * @param userId      用户id
     * @param newNickName 新用户名
     */
    @Update("update user set nick_name=#{newNickName} where user_id=#{userId}")
    void editUserNickName(Integer userId,@Param("newNickName") String newNickName);

    /**增加用户校区信息
     * @param userId    用户id
     * @param newCampusId 新用户校区id
     */
    @Update("insert into user_campus(user_id,campus_id) values(#{userId},#{newCampusId}) ")
    void addUserCampus(Integer userId,Integer newCampusId);

    /**修改用户校区
     * @param userId    用户id
     * @param newCampusId 新用户校区id
     */
    @Update("update user_campus set campus_id=#{newCampusId} where user_id=#{userId}")
    void editUserCampus(Integer userId,Integer newCampusId);

    /**修改用户手机号
     * @param userId   用户id
     * @param newPhone 新手机号码
     */
    @Update("update user set phone=#{newPhone} where user_id=#{userId}")
    void editUserPhone(Integer userId,String newPhone);

    /**修改用户微信号
     * @param userId   用户id
     * @param newWechat 新微信号
     */
    @Update("update user set wechat_id=#{newWechat} where user_id=#{userId}")
    void editUserWechat(Integer userId,String newWechat);

    /**获得用户发布的商品个数
     * @param userId 用户id
     * @return Integer 用户发布的商品个数
     */
    @Select("select count(*) from product where product.user_id = #{userId}")
    Integer iReleased(Integer userId);

    /**
     * 记录用户点击回复我的最后的时间
     * @param userId 用户id
     * @param lastCommentTime 用户点击回复我的最后的时间
     */
    @Insert("insert into user_comment_time(user_id,last_comment_time) values(#{userId},#{lastCommentTime}) ON DUPLICATE KEY UPDATE last_comment_time = #{lastCommentTime}")
    void saveLastCommentTime(Integer userId, Date lastCommentTime);


    /**
     * 获取上次点击回复我的时间
     * @param userId 用户id
     * @return Date 回复我的最后时间
     */
    @Select("select last_comment_time from user_comment_time where user_id=#{userId} ")
    Date getLastCommentTime(Integer userId);


    /**获取回复我的具体评论信息
     * @param userId 用户id
     * @return List<Reply> 具体评论信息
     */
    @Select("SELECT compress_img,nick_name,avatar_url,create_time,content FROM comment c,product_compressimg pc,user u " +
            "where target_comment_id in (SELECT comment_id FROM `comment` WHERE owner_user_id =#{userId}) " +
            " and owner_user_id != #{userId} " +
            " and pc.product_id = c.product_id " +
            " and owner_user_id = u.user_id")
    @Results(id = "replyMap",value = {
            @Result(property = "nickName",column = "nick_name"),
            @Result(property = "avatarUrl",column = "avatar_url"),
            @Result(property = "createTime",column = "create_time"),
            @Result(property = "content",column = "content"),
            @Result(property = "compressImgs"
                    , column = "product_id"
                    , javaType = java.util.List.class
                    , many = @Many(select = "com.fosu.dao.CompressImgDao.findImgsById"))
    })
    List<Reply> getReplyMe(Integer userId);

}
